﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CustomsSystem.Model;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data.Common;
using System.Collections.Generic;
using Maticsoft.DBUtility;

namespace CustomsSystem.DAL
{
    /// <summary>
    /// 数据访问类:UserService
    /// </summary>
    public partial class UserService
    {
        public static string connectionString =@"Data Source=.;Initial Catalog=Customs;trusted_connection=SSPI" ;
        
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return GetMaxID("Id", "Cu_User");
        }
        public static int GetMaxID(string FieldName, string TableName)
        {
            
            string strsql = "select max(" + FieldName + ")+1 from " + TableName;

            object obj =DbHelperSQL . GetSingle(strsql);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return int.Parse(obj.ToString());
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Cu_User");
            strSql.Append(" where id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = id ;

            return DbHelperSQL .Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistName(string UserName)
        {
            
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Cu_User");
            strSql.Append(" where UserName=@UserName");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar ,20)
};
            parameters[0].Value = UserName;

            return DbHelperSQL . Exists(strSql.ToString());
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CustomsSystem.Model.Users  model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Cu_User(");
            strSql.Append("EmpNum,UserName,PassWord,JobNo,Remarks)");
            strSql.Append(" values (");
            strSql.Append("@EmpNum,@UserName,@PassWord,@JobNo,@Remarks)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@EmpNum", SqlDbType.VarChar,4),
					new SqlParameter("@UserName", SqlDbType.VarChar,20),
					new SqlParameter("@PassWord", SqlDbType.VarChar,20),
					new SqlParameter("@JobNo", SqlDbType.VarChar,20),
                    new SqlParameter("@Remarks", SqlDbType.VarChar,100),
                                        };
            parameters[0].Value = model.EmpNum ;
            parameters[1].Value = model.UserName  ;
            parameters[2].Value = model.PassWord ;
            parameters[3].Value = model.JobNo ;
            parameters[3].Value = model.Remarks;
            

            object obj =DbHelperSQL . GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CustomsSystem.Model.Users model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Cu_User set ");
            strSql.Append("EmpNum=@EmpNum,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("PassWord=@PassWord,");
            strSql.Append("JobNo=@JobNo");
            strSql.Append(" where id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@EmpNum", SqlDbType.VarChar,4),
					new SqlParameter("@UserName", SqlDbType.VarChar,20),
					new SqlParameter("@PassWord", SqlDbType.VarChar,20),
					new SqlParameter("@JobNo", SqlDbType.VarChar,20),
					new SqlParameter("@Id", SqlDbType.Int,4)};
              parameters[0].Value = model.EmpNum ;
            parameters[1].Value = model.UserName  ;
            parameters[2].Value = model.PassWord ;
            parameters[3].Value = model.JobNo ;
            parameters[4].Value = model.Id;

            int rows =DbHelperSQL . ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Cu_User ");
            strSql.Append(" where id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows =DbHelperSQL . ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Cu_User ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows =DbHelperSQL . ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Users GetModel(string  EMPNUM)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 EmpNum,UserName,PassWord,JobNo from Cu_User  ");
            strSql.Append(" where EmpNum="+EMPNUM );
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = EMPNUM;

           CustomsSystem.Model.Users  model = new CustomsSystem.Model.Users();
            DataSet ds =DbHelperSQL . Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
               // if (ds.Tables[0].Rows[0]["id"].ToString() != "")
              //  {
               //     model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
              //  }
                if (ds.Tables[0].Rows[0]["EmpNum"] != null)
                {
                    model.EmpNum = ds.Tables[0].Rows[0]["EmpNum"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserName"] != null)
                {
                    model.UserName  = ds.Tables[0].Rows[0]["UserName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PassWord"] != null)
                {
                    model.PassWord  = ds.Tables[0].Rows[0]["PassWord"].ToString();
                }
                if (ds.Tables[0].Rows[0]["JobNo"] != null)
                {
                    model.JobNo  = ds.Tables[0].Rows[0]["JobNo"].ToString();
                }
               
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Users GetModel(string UserName, string PassWord)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,EmpNum,UserName,PassWord,JobNo from Cu_User ");
            strSql.Append(" where  (UserName=@UserName or EMPNUM=@UserName)  and PassWord=@PassWord");
            SqlParameter[] parameters = {
						new SqlParameter("@UserName", SqlDbType.VarChar,20),
					new SqlParameter("@PassWord", SqlDbType.VarChar,32)
                                        };
            parameters[0].Value = UserName;
            parameters[1].Value = PassWord;

            CustomsSystem.Model.Users model = new CustomsSystem.Model.Users();
            DataSet ds =DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EmpNum"] != null)
                {
                    model.EmpNum = ds.Tables[0].Rows[0]["EmpNum"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserName"] != null)
                {
                    model.UserName  = ds.Tables[0].Rows[0]["UserName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PassWord"] != null)
                {
                    model.PassWord  = ds.Tables[0].Rows[0]["PassWord"].ToString();
                }
                if (ds.Tables[0].Rows[0]["JobNo"] != null)
                {
                    model.JobNo  = ds.Tables[0].Rows[0]["JobNo"].ToString();
                }
               
                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 根据用户名得到用户ID
        /// </summary>
        public object GetSingle(string SQLString)
        {
            SQLString = string.Format("select EMPNum from Cu_User where UserName = '{0}'", SQLString);
            return DbHelperSQL.GetSingle(SQLString);
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select ID,EmpNum,UserName,JobNo,Remarks ");
            strSql.Append(" FROM Cu_User ");
          //  strSql.Append(" where UserName !='admin' and UserName !='test' ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" "+strWhere);
            } 
            return DbHelperSQL .Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetMyList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select ID,EmpNum,UserName,JobNo,Remarks ");
            strSql.Append(" FROM Cu_User ");
            //  strSql.Append(" where UserName !='admin and UserName !='test' ");
            if (strWhere.Trim() != "")
            {
                strSql.Append("where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,EmpNum,UserName,JobNo,Remarks ");
            strSql.Append(" FROM Cu_User ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL .Query(strSql.ToString());
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
    /*  public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
            parameters[0].Value = "users";
            parameters[1].Value = "uid";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
        }*/



      


        #endregion  Method
    }
}

